package jhh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jhh.entity.Jtl;
import jhh.service.JtlReader;


@WebServlet("/AnalysisJtl")
public class AnalysisJtl extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw=response.getWriter();
		String path=this.getServletContext().getRealPath("result/jmeter_result.jtl");
		JtlReader reader=new JtlReader();
		List<Jtl> list=reader.JtlReader(path);
		HttpSession session=request.getSession();
		session.setAttribute("jtl", list);
		String json = Test.JSON.Encode(list);
		pw.write(json);
		/*int m=0,n=0,k=0;
		int a=0,b=0,c=0,d=0;
		for (Jtl jtl : list) {
			if(jtl.isResult()==true){
				m++;
				k++;
				if(jtl.getT()<200){a++;}
				else if(jtl.getT()<500){b++;}
				else if(jtl.getT()<2000){c++;}
				else{d++;}
			}else{
				n++;
				k++;
				pw.write(jtl.getTn()+" "+jtl.getJtlInfo()+"<br/>");
				
			}
			
		}
		pw.write("总数为"+k+"<br/>");
		pw.write("成功个数为"+m+"<br/>");
		pw.write("失败个数为"+n+"<br/>");
		pw.write("成功的其中<br/>");
		pw.write("响应时间小于200的个数为"+a+"<br/>");
		pw.write("响应时间在200到500之间的个数为"+b+"<br/>");
		pw.write("响应时间在500到2000之间的个数为"+c+"<br/>");
		pw.write("响应时间大于2000的个数为"+d+"<br/>");
		
		double m1=m;
		double k1=k;
		double i=(m1/k1)*100.0;
		
		pw.write("成功率为"+i+"%");*/
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
